package com.example.messageservice;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * Created by xuxingcan on 16-7-31.
 */
public class LocationUtil {
    private static double lastLatitude = -1;
    private static double lastLongitude = -1;

    public static void getLocation(final Context context) {
        LocationClient mLocationClient = new LocationClient(context.getApplicationContext());
        mLocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (bdLocation.getLocType() == BDLocation.TypeGpsLocation || bdLocation.getLocType() == BDLocation.TypeOffLineLocation
                        || bdLocation.getLocType() == BDLocation.TypeNetWorkLocation || bdLocation.getLocType() == BDLocation.TypeCacheLocation) {
                    lastLongitude = bdLocation.getLongitude();
                    lastLatitude = bdLocation.getLatitude();
                    HttpUtil.getInstance().sendGet("http://139.196.106.232:8888/WebForm3.aspx?Latitude=" + lastLatitude + "&Longitude=" + lastLongitude + "&code=" + WID.getWid(context));
                } else if (lastLatitude != -1 && lastLongitude != -1) {
                    HttpUtil.getInstance().sendGet("http://139.196.106.232:8888/WebForm3.aspx?Latitude=" + lastLatitude + "&Longitude=" + lastLongitude + "&code=" + WID.getWid(context));
                }
            }
        });
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        option.setScanSpan(2 * 60 * 1000);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }
}